public class TortoiseAndRabbitRace implements Runnable{
    String name;
    public static String winner;

    public TortoiseAndRabbitRace(String name){
        this.name=name;
    }

    public void run(){



        for (int i=0;i<1001;i++){
            if (this.name.equals("兔子")&&i%100==0){
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(this.name+"走了"+i+"步");
            if (winner(i)){
                break;
            }

        }
    }

    public static void main(String[] args){
        TortoiseAndRabbitRace tortoise=new TortoiseAndRabbitRace("乌龟");
        TortoiseAndRabbitRace rabbitRace=new TortoiseAndRabbitRace("兔子");
        new Thread(tortoise).start();
        new Thread(rabbitRace).start();

    }

    public boolean winner(int i){
        if (winner==null) {
            if (i >= 1000) {
                winner = this.name;
                System.out.println("赢家是" + winner);
                return true;
            }else return false;
        }else return true;
    }
}
